<template>
  <el-dialog :visible.sync="dialogTableVisible" :append-to-body="true" :lock-scroll="false" :title="isEdit?'':''" width="90%" @close="handleCloseDetailDialog">
    <template>
      <div class="app-container">
        <!--<div class="filter-container">-->
        <!--<el-select v-model="listQuery.searchKey" :placeholder="$t('table.search')" clearable style="width: 90px" class="filter-item">-->
        <!--<el-option v-for="item in searchOptions" :key="item.value" :label="item.label" :value="item.value"/>-->
        <!--</el-select>-->
        <!--<el-input v-model="listQuery.searchValue" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"/>-->
        <!--&lt;!&ndash;<el-select v-model="listQuery.is_available" :placeholder="$t('table.available')" clearable style="width: 90px" class="filter-item">&ndash;&gt;-->
        <!--&lt;!&ndash;<el-option v-for="(item, index) in availableOptions" :key="index" :label="item.label" :value="item.value"/>&ndash;&gt;-->
        <!--&lt;!&ndash;</el-select>&ndash;&gt;-->
        <!--&lt;!&ndash;<el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>&ndash;&gt;-->
        <!--&lt;!&ndash;<el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-share" @click="handleCreate">{{ $t('table.add') }}</el-button>&ndash;&gt;-->
        <!--</div>-->
        <el-table
          :data="list"
          border
          fit
          highlight-current-row
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            :label="$t('table.id')"
            align="center"
            width="50">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('table.available')"
            align="center"
            width="100px">
            <template slot-scope="scope">
              <span>{{ scope.row.available_name }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('usercouponfiles.is_used')"
            align="center"
            width="100px">
            <template slot-scope="scope">
              <span>{{ scope.row.status_name }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :show-overflow-tooltip="true"
            :label="$t('usercouponfiles.code')"
            align="center"
            width="150px">
            <template slot-scope="scope">
              <span>{{ scope.row.code }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :show-overflow-tooltip="true"
            :label="$t('usercouponfiles.order_no')"
            align="center"
            width="150px">
            <template slot-scope="scope">
              <span>{{ scope.row.order_no }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :show-overflow-tooltip="true"
            :label="$t('usercouponfiles.notice')"
            align="center"
            width="150px">
            <template slot-scope="scope">
              <span>{{ scope.row.notice_name }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('login.username')"
            align="center"
            min-width="160px">
            <template slot-scope="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('couponfiles.discount_type')"
            align="center"
            min-width="160px">
            <template slot-scope="scope">
              <span>{{ scope.row.discount_type }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('table.used_time')"
            align="center"
            min-width="100px">
            <template slot-scope="scope">
              <span>{{ scope.row.used_time }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('table.actions')"
            align="center"
            width="300">
            <template slot-scope="scope">
              <!--<el-button type="primary" size="small" class="el-icon-edit" @click="handleUpdate(scope.row.id)">{{ $t('table.edit') }}</el-button>-->
              <el-button type="danger" size="small" class="el-icon-delete" @click="destroyRecord(scope.row)">{{ $t('table.delete') }}</el-button>
              <el-button type="primary" size="small" class="el-icon-edit" :disabled="scope.row.notice !== 0" @click="sendemail(scope.row.id)" >SendEmail</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="sendallnmail()">SendEmail</el-button>
        </div>

        <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :pagesize.sync="listQuery.pagesize" @pagination="indexRecord" />

      </div>
    </template>
  </el-dialog>
</template>
<script>

import usercouponApi from '@/api/coupon/usercoupon'
import i18n from '@/lang/index'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Coupons',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogTableVisible: false,
      listQuery: {
        lang: i18n.locale,
        page: 1,
        pagesize: 20,
        is_available: 2,
        name: '',
        sort: 'desc',
        orderby: 'id',
        searchKey: 'user_id',
        searchValue: '',
        couponid: 0
      },
      availableOptions: [
        { 'label': i18n.t('table_element.available.all'), 'value': 2 },
        { 'label': i18n.t('table_element.available.on'), 'value': 1 },
        { 'label': i18n.t('table_element.available.un'), 'value': 0 }
      ],
      editId: 0,
      isEdit: false,
      searchOptions: [
        { value: 'user_id', label: i18n.t('usercouponfiles.user_id') },
        { value: 'coupon_id', label: i18n.t('usercouponfiles.coupon_id') }
      ],
      selectusercoupon: []
    }
  },
  created() {
    this.indexRecord()
  },
  methods: {

    handleFilter() {
      this.listQuery.page = 1
      this.indexRecord()
    },

    handleCreate() {
      this.$refs.detail.handShowDetailDialog(0)
    },

    handleUpdate(id) {
      this.$refs.detail.handShowDetailDialog(id)
    },
    indexRecord() {
      this.listLoading = true
      usercouponApi.indexRecord(this.listQuery).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.list = serviceResponse.data.data
          this.total = serviceResponse.data.total
        }
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },

    destroyRecord(row) {
      this.$confirm('Confirm deletion?', 'Message', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        usercouponApi.destroyRecord(row.id).then(response => {
          const serviceResponse = response.data
          if (serviceResponse.code === 0) {
            this.$message({
              type: 'success',
              message: 'success!'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          } else {
            this.$message({
              type: 'error',
              message: 'error!'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: ' Canceled deletion'
        })
      })
    },

    handShowDetailDialog(id) {
      this.indexRecord()
      this.listQuery.couponid = id
      this.dialogTableVisible = true
    },
    handleCloseDetailDialog() {
      this.dialogTableVisible = false
    },

    sendallnmail() {
      const params = {
        'usercouponids': this.selectusercoupon
      }
      usercouponApi.sendcouponemail(params).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.$message({
            type: 'success',
            message: 'success!'
          })
        } else {
          this.$message({
            type: 'info',
            message: 'error'
          })
        }
      })
    },

    sendemail(id) {
      this.listLoading = true
      const ids = [id]
      const params = {
        'usercouponids': ids
      }
      usercouponApi.sendcouponemail(params).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.$message({
            type: 'success',
            message: 'success!'
          })
        } else {
          this.$message({
            type: 'info',
            message: 'error'
          })
        }
      })
      this.indexRecord()
      setTimeout(() => {
        this.listLoading = false
      }, 1.5 * 1000)
    },
    handleSelectionChange(arr) {
      let len = len = arr.length

      const needarray = []

      for (var i = 0; i < len; i++) {
        needarray.push(arr[i].id)
      }
      //console.log(needarray)

      this.selectusercoupon = needarray
    }

  }
}

</script>
